//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pt;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.PssProductCodeDao;
import com.aidong.dao.wills.PtItemCodeDao;
import com.aidong.dao.wills.PtPackageDao;
import com.aidong.dao.wills.PtPackageItemDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.PssProductCode;
import com.aidong.model.wills.PtPackage;
import com.aidong.model.wills.PtPackageItem;
import com.aidong.web.controller.pss.PopProductController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptPackage"})
public class PtPackageController extends PopProductController {
    @Resource
    PtPackageDao ptPackageDao;
    @Resource
    PtPackageItemDao ptPackageItemDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    PtItemCodeDao ptItemCodeDao;
    @Resource
    PssProductCodeDao pssProductCodeDao;

    public PtPackageController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("pt/package/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        ConditionMap cd1 = new ConditionMap(1);
        if(!this.chkNull(name)) {
            cd1.like("code", "%" + name + "%");
            cd1.like("chName", "%" + name + "%");
            cd.addSub(cd1);
        }

        this.ptPackageDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv = new ModelAndView("pt/package/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        PtPackage pkg = new PtPackage();
        pkg.setStatus(Integer.valueOf(1));
        List shopList = this.dicShopCodeDao.getAllShopList();
        Iterator pt = shopList.iterator();

        while(pt.hasNext()) {
            DicShopCode mv = (DicShopCode)pt.next();
            mv.setStatus(Integer.valueOf(0));
        }

        PtPackageItem pt1 = new PtPackageItem();
        ModelAndView mv1 = new ModelAndView("pt/package/edit");
        mv1.addObject("pkg", pkg);
        mv1.addObject("pt", pt1);
        mv1.addObject("shopList", shopList);
        mv1.addObject("ptCodeList", this.ptItemCodeDao.getValidItemList());
        mv1.addObject("coachLevel", this.getTypeList("coachLevel"));
        mv1.addObject("_oper", "add");
        return mv1;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("pkg") PtPackage pkg, @ModelAttribute("pt") PtPackageItem pt, @RequestParam(value = "items",required = false) String items, @RequestParam(value = "shops",required = false) String shops) {
        if(this.chkNull(pkg.getCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(pkg.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNullZ(pkg.getTotalPrice())) {
            return this.getResult(false, "请输入总价");
        } else {
            if(this.chkNullZ(pkg.getStatus())) {
                pkg.setStatus(Integer.valueOf(1));
            }

            if(this.ptPackageDao.exists(pkg.getCode())) {
                return this.getResult(false, "编号已存在");
            } else {
                pkg.setCreateUser(this.getLoginUserCode());
                pkg.setCreateTime(new Date());
                pkg.setUpdateTime(new Date());
                List itemList = this.getPackageItem(pt, items);
                if(itemList != null && itemList.size() >= 2) {
                    ArrayList shopList = new ArrayList();
                    if(!this.chkNull(shops)) {
                        String[] price = shops.split(",");
                        String[] arr$ = price;
                        int e = price.length;

                        for(int item = 0; item < e; ++item) {
                            String s = arr$[item];
                            if(!this.chkNull(s)) {
                                shopList.add(s.trim());
                            }
                        }
                    }

                    double var13 = 0.0D;
                    Iterator var14 = itemList.iterator();

                    while(var14.hasNext()) {
                        PtPackageItem var15 = (PtPackageItem)var14.next();
                        if(!"-".equals(var15.getPackageCode())) {
                            if(this.chkNull(var15.getPrice())) {
                                return this.getResult(false, "商品价格错误");
                            }

                            var13 += var15.getPrice().doubleValue();
                        }
                    }

                    if(!this.equals(pkg.getTotalPrice().doubleValue(), var13)) {
                        return this.getResult(false, "总价格不一致");
                    } else {
                        try {
                            this.ptPackageDao.addPtPackage(pkg, itemList, shopList);
                            this.addUserLog("新增私教礼包", "编号:" + pkg.getCode() + ",名称:" + pkg.getChName());
                        } catch (Exception var12) {
                            this.log.error("add ptpackage", var12);
                            return this.getResult(false, "保存发生错误");
                        }

                        return this.getResult(true, "保存完成");
                    }
                } else {
                    return this.getResult(false, "商品数据错误");
                }
            }
        }
    }

    private List<PtPackageItem> getPackageItem(PtPackageItem pt, String times) {
        ArrayList list = new ArrayList();
        pt.setItemType("1");
        list.add(pt);
        if(!this.chkNull(times)) {
            String[] ss = times.split("\\|");
            String[] arr$ = ss;
            int len$ = ss.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String s1 = arr$[i$];
                String[] s = s1.split(",");
                Integer id = this.parseInt(s[0]);
                PtPackageItem item;
                if(s.length == 2 && "-".equals(s[1]) && !this.chkNullZ(id)) {
                    item = new PtPackageItem(id);
                    item.setPackageCode("-");
                } else {
                    if(s.length <= 3) {
                        return null;
                    }

                    item = new PtPackageItem(id);
                    item.setItemType("2");
                    item.setItemCode(s[1]);
                    item.setQuantity(this.parseInt(s[2]));
                    item.setPrice(this.parseDouble(s[3]));
                }

                list.add(item);
            }
        }

        return list;
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("code") String code) {
        PtPackage pkg = (PtPackage)this.ptPackageDao.getById(code);
        if(pkg == null) {
            return this.showAlert("没有这个记录");
        } else {
            PtPackageItem pt = null;
            List ptList = this.ptPackageItemDao.getPtPackageItemList(pkg.getCode(), "1");
            if(ptList != null && ptList.size() > 0) {
                pt = (PtPackageItem)ptList.get(0);
            }

            List itemList = this.ptPackageItemDao.getPtPackageItemList(pkg.getCode(), "2");
            List shopList = this.ptPackageDao.getPtPackageShopList(code, true);
            ModelAndView mv = new ModelAndView("pt/package/edit");
            mv.addObject("pkg", pkg);
            mv.addObject("pt", pt);
            mv.addObject("itemList", itemList);
            mv.addObject("shopList", shopList);
            mv.addObject("ptCodeList", this.ptItemCodeDao.getValidItemList());
            mv.addObject("coachLevel", this.getTypeList("coachLevel"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("pkg") PtPackage pkg, @ModelAttribute("pt") PtPackageItem pt, @RequestParam(value = "items",required = false) String items, @RequestParam(value = "shops",required = false) String shops) {
        if(this.chkNull(pkg.getCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(pkg.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNullZ(pkg.getTotalPrice())) {
            return this.getResult(false, "请输入总价");
        } else {
            if(this.chkNullZ(pkg.getStatus())) {
                pkg.setStatus(Integer.valueOf(1));
            }

            PtPackage pkg1 = (PtPackage)this.ptPackageDao.getById(pkg.getCode());
            if(pkg1 == null) {
                return this.getResult(false, "没有这个记录");
            } else {
                pkg1.setChName(pkg.getChName());
                pkg1.setTotalPrice(pkg.getTotalPrice());
                pkg1.setActiveTime(pkg.getActiveTime());
                pkg1.setInactiveTime(pkg.getInactiveTime());
                pkg1.setNote(pkg.getNote());
                pkg1.setStatus(pkg.getStatus());
                pkg1.setUpdateTime(new Date());
                List itemList = this.getPackageItem(pt, items);
                if(itemList != null && itemList.size() >= 2) {
                    ArrayList shopList = new ArrayList();
                    if(!this.chkNull(shops)) {
                        String[] price = shops.split(",");
                        String[] arr$ = price;
                        int e = price.length;

                        for(int item = 0; item < e; ++item) {
                            String s = arr$[item];
                            if(!this.chkNull(s)) {
                                shopList.add(s.trim());
                            }
                        }
                    }

                    double var14 = 0.0D;
                    Iterator var15 = itemList.iterator();

                    while(var15.hasNext()) {
                        PtPackageItem var16 = (PtPackageItem)var15.next();
                        if(!"-".equals(var16.getPackageCode())) {
                            if(this.chkNull(var16.getPrice())) {
                                return this.getResult(false, "商品价格错误");
                            }

                            var14 += var16.getPrice().doubleValue();
                        }
                    }

                    if(!this.equals(pkg.getTotalPrice().doubleValue(), var14)) {
                        return this.getResult(false, "总价格不一致");
                    } else {
                        try {
                            this.ptPackageDao.updatePtPackage(pkg1, itemList, shopList);
                            this.addUserLog("修改私教礼包", "编号:" + pkg.getCode() + ",名称:" + pkg.getChName());
                        } catch (Exception var13) {
                            this.log.error("edit ptpackage", var13);
                            return this.getResult(false, "保存发生错误");
                        }

                        return this.getResult(true, "保存完成");
                    }
                } else {
                    return this.getResult(false, "商品数据错误");
                }
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        PtPackage pkg = (PtPackage)this.ptPackageDao.getById(code);
        if(pkg == null) {
            return this.getResult(false, "没有这个记录");
        } else {
            try {
                this.ptPackageDao.updateStatus(code, 0);
                this.addUserLog("删除私教礼包", "编号:" + pkg.getCode() + ",名称:" + pkg.getChName());
            } catch (Exception var4) {
                this.log.error("del ptpackage", var4);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }

    @RequestMapping({"getProductInfo"})
    @ResponseBody
    public String getProductInfo(@RequestParam("code") String code) {
        PssProductCode product = (PssProductCode)this.pssProductCodeDao.getById(code);
        if(product == null) {
            return this.getResult(false, "商品编号错误");
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append("{code:\"").append(product.getProductCode()).append("\"").append(",name:\"").append(product.getProductName()).append("\"").append(",price:").append(product.getPrice()).append("").append("}");
            return this.getSuccess(sb.toString(), "");
        }
    }
}
